import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_give/utils/LogUtils.dart';
import 'package:oktoast/oktoast.dart';
import 'package:rapid_kit/rapid_kit.dart';

import '../../theme.dart';

/// 重启设备页面
class EqrestartPage extends StatefulWidget {
  final String deviceId;

  const EqrestartPage({super.key, required this.deviceId});

  @override
  State<StatefulWidget> createState() {
    return _EqrestartPageState();
  }
}

class _EqrestartPageState extends State<EqrestartPage> {
  late String _deviceId;

  // 指令示例
  late Pipe _pipe;


  @override
  void initState() {
    super.initState();
    _deviceId = widget.deviceId;

    LogUtils.w("设备ID${_deviceId}");

    // 创建实例
    _pipe = Pipe.create(_deviceId);
    // 连接设备
    _pipe.establish();

    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Color(0xFFffffff),
      statusBarIconBrightness: Brightness.dark,
    ));

  }

  @override
  void dispose() {
    super.dispose();
    // 销毁实例
    _pipe.abolish();
    _pipe.destroy();
  }

  void _onReboot() async {
    if (!_pipe.isEstablished) return;
    LogUtils.w("========执行=======");
    final resp = await InstructStandard.reboot(_pipe);
    if (!mounted) return;
    if (resp.success) {
      showToast("设备重启成功");
    } else {
      showToast("涉笔重启失败");
    }
  }

  Future<bool> _onWillPop() async {
    // HomePageState.globalKeytwo.currentState?.reloadData();
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        backgroundColor: Color(0xFFF2F3F5),
        appBar: _buildTitleBar(context),
        body: Padding(
          padding: EdgeInsets.all(16.0), // 这里设置内边距，可以根据需要调整数值
          child: Column(
            children: [
              Spacer(),
              // 添加本地图片
              _buildreportRow(),
              Spacer()
            ],
          ),
        ),
      ),
      onWillPop: _onWillPop,
    );
  }

  Widget _buildreportRow() {
    return Container(
      decoration: BoxDecoration(),
      padding: EdgeInsets.all(5),
      child: Column(
        children: [
          // 第一行：圆形按钮
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                GestureDetector(
                  onTap: () {
                    // 在这里处理点击事件
                    print('Container 被点击了');
                    _onReboot();
                  },
                  child: Container(
                    width: 150,
                    height: 150,
                    decoration: BoxDecoration(
                      color: Color(0xFF05ccb7),
                      shape: BoxShape.circle,
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.refresh, color: Colors.white, size: 40),
                        SizedBox(height: 4),
                        Text(
                          '点击重启',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 15,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 30),
          // 第二行：文字提示
          Padding(
            padding: EdgeInsets.all(8.0),
            child: Text(
              '重启会导致设备短暂掉线不可用',
              style: TextStyle(
                color: Colors.black,
                fontSize: 13,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  // 导航
  AppBar _buildTitleBar(BuildContext context) {
    return AppBar(
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          _onWillPop();
          Navigator.of(context).pop();
        },
      ),
      automaticallyImplyLeading: false,
      title: Text(
        "重启设备",
        style: TextStyle(
          fontSize: 17,
          fontFamily: "PingFang SC",
          fontWeight: FontWeight.w600,
        ),
      ),
      centerTitle: true,
    );
  }
}
